package com.yc.class_attendance.net;


import com.yc.class_attendance.MyApp;
import com.yc.class_attendance.entity.AttMeetingResutl;
import com.yc.class_attendance.entity.AttendanceResutl;
import com.yc.class_attendance.entity.ImageResutl;
import com.yc.class_attendance.entity.Resutl;
import com.yc.class_attendance.entity.UploadResutl;
import com.yc.class_attendance.utils.LogInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by sunhellc on 16/6/13.
 */
public class Api {
   // public static final String BASE_URL = "http://10.10.8.173:8080";
    private static final int DEFAULT_TIMEOUT = 5;
     public static final String BASE_URL ="http://192.168.10.11:8080";//"http://10.10.8.136:8080";//http://10.10.8.136:8080";//http://10.10.8.136:9300//http://192.168.10.11:8080";//http://10.10.8.207:8080";// "http://10.10.8.136:9300";//
    private Retrofit retrofit;
    private Service service;


    //构造方法私有
    private Api() {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().addInterceptor(new LogInterceptor());
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(MyApp.getsInstance().sharePreferenceUtil.getServerAddress())
                .build();

        service = retrofit.create(Service.class);
    }


    //在访问HttpMethods时创建单例
    private static class SingletonHolder {
        private static final Api INSTANCE = new Api();
    }

    //获取单例
    public static Api getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private <T> void toSubscribe(Observable<T> o, Subscriber<T> s) {
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }

    /**
     * 查询课程
     *
     * @param subscriber
     * @param termCode
     */
    public void GetMeeting(Subscriber<AttendanceResutl> subscriber, String termCode, String time) {
        Observable observable = service.Getmeeting(termCode, time);
        toSubscribe(observable, subscriber);
    }

    /**
     * 查询课程
     *
     * @param subscriber
     * @param termCode
     */
    public void GetAttMeeting(Subscriber<AttMeetingResutl> subscriber, String termCode, String time) {
        Observable observable = service.GetAttmeeting(termCode, time);
        toSubscribe(observable, subscriber);
    }


    /**
     * 上传考勤信息
     * @param subscriber
     * @param json
     */
    public void UploadCheck(Subscriber<Resutl> subscriber, String json) {
        Observable observable = service.upload( json);
        toSubscribe(observable, subscriber);

    }

    /**
     * 更新
     */
    public  void  UploadApk(Subscriber<UploadResutl> subscriber){
        Observable observable = service.uploadApk();
        toSubscribe(observable, subscriber);
    }

    /**
     *  上传照片
     * @param subscribe
     * @param imageTitle
     * @param imageIntro
     * @param actId
     * @param userId
     */
    public  void  UploadImageString(Subscriber<ImageResutl>subscribe , String imageTitle, String imageIntro, String actId, String userId ){
    Observable observable = service.UploadImageString(imageTitle,imageIntro,actId,userId);
    toSubscribe(observable,subscribe);
}
}
